// pages/user.js
Page({
  data: {
    active: 0,
    neighbor: {},
    typeList: [{
      key: 1,
      value: "心情动态"
    }, {
      key: 2,
      value: "自家售卖"
    }, {
      key: 3,
      value: "出租招租"
    }, {
      key: 5,
      value: "二手商品"
    }, {
      key: 6,
      value: "旅游代买"
    }, {
      key: 7,
      value: "技能墙"
    }],
    type: "2",
    list: [],
    page: 1,
    size: 10,
    isNoMore: true,
    isLoding: true
  },
  onLoad: function (options) {
    this.setData({
      neighbor: wx.getStorageSync("neighbor")
    });
    this.getList();
  },
  tab(e) {
    this.setData({
      active: e.currentTarget.dataset.index,
      type: e.currentTarget.dataset.index == 0 ? "2" : "1",
      list: [],
      page: 1,
      isNoMore: true,
      isLoding: true
    });
    this.getList();
  },
  typeSel(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      list: [],
      page: 1,
      isNoMore: true,
      isLoding: true
    });
    this.getList();
  },
  thumbup(e) {
    getApp().globalData.http.thumbup({
      id: e.currentTarget.dataset.id
    }).then(res => {
      this.setData({
        ["list[" + e.currentTarget.dataset.index + "].isThumbsUp"]: 1,
        ["list[" + e.currentTarget.dataset.index + "].thumbsUp"]: this.data.list[e.currentTarget.dataset.index].thumbsUp / 1 + 1,
      });
    });
  },
  thumbupList(e) {
    wx.navigateTo({
      url: "/pages/my/likedlist/likedlist?id=" + e.currentTarget.dataset.id,
    });
  },
  previewImg(e) {
    let arr = e.currentTarget.dataset.img;
    let current = e.currentTarget.dataset.current;
    getApp().globalData.util.previewImage(arr, current);
  },
  getList() {
    if (this.data.active == 0) {
      let params = {
        page: this.data.page,
        size: this.data.size,
        saleStatus: 1,
        type: this.data.type,
        userId: this.data.neighbor.userId
      }
      getApp().globalData.http.myStall(params).then(res => {
        let data_tmp = [...this.data.list, ...res.data];
        this.setData({
          list: data_tmp,
          isLoding: true
        });
        if (this.data.list.length == res.count) {
          this.setData({
            isNoMore: false
          });
        }
      });
    } else if (this.data.active == 1) {
      let params = {
        page: this.data.page,
        size: this.data.size,
        type: this.data.type,
        userId: this.data.neighbor.userId
      };
      getApp().globalData.http.myNews(params).then(res => {
        let data_tmp = [...this.data.list, ...res.data];
        this.setData({
          list: data_tmp,
          isLoding: true
        });
        if (this.data.list.length == res.count) {
          this.setData({
            isNoMore: false
          });
        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      });
    }
  },
  toDestails(e) {
    wx.navigateTo({
      url: "/pages/common/gooddetail/gooddetail?id=" + e.currentTarget.dataset.id,
    })
  }
})